import * as types from '../mutationType'

const state = {
    cartList: []
};

const getters = {
    getCartList: state => state.cartList
};

const mutations = {
    [types.ADD_TO_CART](state, {id, name, price, specifications}) {
        var record = state.cartList.find(item => item.id === id);
        if (!record) {
            state.cartList.push({id, name, price, specifications, quantity: 1, total: price});
        } else {
            record.quantity++;
            record.total = record.price * record.quantity;
        }
    },
    [types.REMOVE_TO_CART](state, id) {
      var record = state.cartList.find(item=>item.id === id);
   
      if (record.quantity > 1) {
          record.quantity--;
          record.total -= record.price;
      } else {
          state.cartList=state.cartList.filter(item=>item.id !== record.id)
      }
    },
}
export default{
    state,
    getters,
    mutations
}